﻿using Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
    public class DBHelper
    {
        public static string str = "server=.;database=Seagull;integrated security=true;";
        public static SqlConnection conn = new SqlConnection(str);
        public DataTable GetDataTable(string sql)
        {
            SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            return dt;
        }
        public DataTable GetDataTableByPage(string cpName, PageModel pageModel, ref int TotalRows)
        {
            DataTable dt = new DataTable();
            conn.Open();
            SqlCommand cmd = new SqlCommand(cpName, conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@primarykey", SqlDbType.NVarChar, 50).Value = pageModel.PrimaryKey;
            cmd.Parameters.Add("@pageSize", SqlDbType.Int, 4).Value = pageModel.PageSize;
            cmd.Parameters.Add("@whereCond", SqlDbType.NVarChar, 50).Value = pageModel.WhereCond;
            cmd.Parameters.Add("@tableName", SqlDbType.NVarChar, 20).Value = pageModel.TableName;
            cmd.Parameters.Add("@orderBy", SqlDbType.NVarChar, 50).Value = pageModel.OrderBy;
            cmd.Parameters.Add("@totalRows", SqlDbType.Int, 4).Direction = ParameterDirection.Output;
            SqlDataAdapter ds = new SqlDataAdapter(cmd);
            ds.Fill(dt);
            TotalRows = int.Parse(cmd.Parameters["@totalRows"].Value.ToString());
            conn.Close();
            return dt;

        }
        public bool Zsg(string sql)
        {
            conn.Open();
            SqlCommand cmd = new SqlCommand(sql, conn);
            int result = cmd.ExecuteNonQuery();
            conn.Close();
            return result > 0;
        }
    }
}
